package com.ymatou.userservice.testcase;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.SetNickNameDtoBean;
import com.ymatou.userservice.service.SetNickNameDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*设置昵称接口
*FreeMaker Template自动生成代码
*后台是根据Ymt_User_LoginIdUpdateHistory表里的更改记录来判断用户是否修改过昵称的
*/
@TestSuite
public class Ts_SetNickNameDto{
	private static SetNickNameDtoBean setnicknamedtoBean;
	private static SetNickNameDtoCall setnicknamedtoCall;
//	private static ULC201406Wapper userWapper;
	
	@BeforeClass
	public static void setUp() throws ClientProtocolException, IOException {
		Logger.createResultFile("设置昵称接口");
	}

	@Before
	public void caseUp() throws Exception {
		setnicknamedtoBean=new SetNickNameDtoBean();
		setnicknamedtoCall=new SetNickNameDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setnicknamedtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNickNameDto_001() {
		Logger.start(true,"修改已存在的用户的昵称，成功");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(10)+"_nickName";
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			ULC201406Wapper userWapper=new ULC201406Wapper();
			ULC201406Bean updatedUser = userWapper.getULCInfoByUid((Integer)user.get("userid"));
			Logger.verifyEquals(newNickName.toLowerCase(), updatedUser.getlBills().getLoginId(), "验证昵称被修改");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_002() {
		Logger.start(false,"修改不存在的用户的昵称，失败");
		try {
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(Integer.MAX_VALUE);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();			

			Logger.verifyEquals("402", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("用户不存在无法修改昵称", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SetNickNameDto_003() {
		Logger.start(false,"输入已使用过的用户昵称，失败");
		try {
			Map user = UserCallService.registerByMobile();
			ULC201406Wapper userWapper=new ULC201406Wapper();
			ULC201406Bean existUser = userWapper.getULCInfoByUid(100);
			setnicknamedtoBean.setNickName(existUser.getlBills().getLoginId());
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();	
			
			Logger.verifyEquals("405", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("登录ID已被他用户使用", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SetNickNameDto_004() {
		Logger.start(true,"多次修改昵称，只有第一次修改成功");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(7)+"_nickName";
			int usreId = (Integer)user.get("userid");
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			Logger.comment("第一次修改昵称");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId(usreId);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals("ok", setnicknamedtoCall.getString("Result"), "验证绑定成功");
			
			Logger.comment("再尝试三次修改昵称");
			for(int i=0; i<3; i++){
				String nickName = TestDataManager.getRandomNum(7)+"_nickName";
				setnicknamedtoCall = new SetNickNameDtoCall();
				setnicknamedtoBean = new SetNickNameDtoBean();
				setnicknamedtoBean.setNickName(nickName);
				setnicknamedtoBean.setUserId(usreId);
				setnicknamedtoCall.setData(setnicknamedtoBean);
				setnicknamedtoCall.callService();
				Logger.verifyEquals("403", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
				Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
				Logger.verifyEquals("已修改过登录昵称", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			}
			ULC201406Wapper userWapper=new ULC201406Wapper();
			ULC201406Bean updatedUser = userWapper.getULCInfoByUid(usreId);
			Logger.verifyEquals(newNickName.toLowerCase(), updatedUser.getlBills().getLoginId(), "验证昵称为第一次修改后的值");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_005() {
		Logger.start(true,"未输入用户昵称，失败");
		try {
			setnicknamedtoBean.setUserId(100);
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();			
			
			Logger.verifyEquals(ECode.ERROR400, setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("错误的请求参数", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_006() {
		Logger.start(false,"昵称为特殊字符，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = "'\"&%* !@~.^\\_n";//21位
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();
			
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录名称（昵称）内容错误（只能使用中、英文、数字和下划线）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_007() {
		Logger.start(false,"昵称以下划线开头，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = "_nickName_";
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录名称（昵称）首末位错误（昵称首位和末位不能是下划线'_'）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_008() {
		Logger.start(false,"昵称包含码头，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = "码头"+TestDataManager.getRandomNum(7);
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录昵称内容中存在特殊的不合法的字符（昵称中带有非法字符）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_009() {
		Logger.start(false,"昵称包含'ymatou'，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(7)+"ymatou";
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录昵称内容中存在特殊的不合法的字符（昵称中带有非法字符）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_010() {
		Logger.start(false,"昵称包含'yangmatou'，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(7)+"yangmatou";
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录昵称内容中存在特殊的不合法的字符（昵称中带有非法字符）", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_011() {
		Logger.start(false,"昵称全是数字，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(7);
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("登录昵称不能全数字错误", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SetNickNameDto_012() {
		Logger.start(false,"昵称长度过长，失败");
		try {
			Map user = UserCallService.registerByMobile();
			String newNickName = TestDataManager.getRandomNum(43);
			Logger.verifyNotEquals(user.get("username"), newNickName, "前置条件：验证旧的昵称和新的昵称不同");
			setnicknamedtoBean.setNickName(newNickName);
			setnicknamedtoBean.setUserId((Integer)user.get("userid"));
			setnicknamedtoCall.setData(setnicknamedtoBean);
			setnicknamedtoCall.callService();				
			Logger.verifyEquals("401", setnicknamedtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("昵称长度4~40位", setnicknamedtoCall.getString("LastErrorMessage"), "验证Msg信息");
			Logger.verifyEquals("false", setnicknamedtoCall.getString("Success"), "验证操作是否成功");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}